/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "animator_option_napi.h"
#include "imageknife_handle_scope.h"
#include "imageknife_trace.h"
#include "napi_parser.h"
#include "napi_utils.h"

namespace ImageKnifePro {

void AnimatorOptionNapi::ParseProperty(napi_env env, napi_value jsObject)
{
    napi_value value = NapiParser::ParseProperty(env, jsObject, "state");
    if (value != nullptr) {
        state = static_cast<ArkUI_AnimationStatus>(NapiParser::ParseInt(env, value));
    }

    value = NapiParser::ParseProperty(env, jsObject, "iterations");
    if (value != nullptr) {
        iterations = NapiParser::ParseInt(env, value);
    }

    value = NapiParser::ParseProperty(env, jsObject, "reverse");
    if (value != nullptr) {
        reverse = NapiParser::ParseBoolean(env, value, reverse);
    }
}

AnimatorOptionNapi::AnimatorOptionNapi(napi_env env, napi_value jsObject)
{
    ImageKnifeTrace trace("AnimatorOptionNapi");
    trace.StartSyncTrace();

    env_ = env;
    napi_create_reference(env, jsObject, 1, &jsThis_);
    ParseProperty(env, jsObject);

    napi_value value = NapiParser::ParseProperty(env, jsObject, "onStart");
    if (value != nullptr) {
        napi_create_reference(env, value, 1, &onStart_);
        onStart = [this]() {this->CallJsFunction(onStart_);};
    }

    value = NapiParser::ParseProperty(env, jsObject, "onFinish");
    if (value != nullptr) {
        napi_create_reference(env, value, 1, &onFinish_);
        onFinish = [this]() {this->CallJsFunction(onFinish_);};
    }

    value = NapiParser::ParseProperty(env, jsObject, "onPause");
    if (value != nullptr) {
        napi_create_reference(env, value, 1, &onPause_);
        onPause = [this]() {this->CallJsFunction(onPause_);};
    }

    value = NapiParser::ParseProperty(env, jsObject, "onCancel");
    if (value != nullptr) {
        napi_create_reference(env, value, 1, &onCancel_);
        onCancel = [this]() {this->CallJsFunction(onCancel_);};
    }

    value = NapiParser::ParseProperty(env, jsObject, "onRepeat");
    if (value != nullptr) {
        napi_create_reference(env, value, 1, &onRepeat_);
        onRepeat = [this]() {this->CallJsFunction(onRepeat_);};
    }
}

void AnimatorOptionNapi::CallJsFunction(napi_ref ref)
{
    ImageKnifeHandleScope scope(env_);

    napi_value thisValue;
    napi_value jsFunc;
    napi_get_reference_value(env_, jsThis_, &thisValue);
    napi_get_reference_value(env_, ref, &jsFunc);
    napi_call_function(env_, thisValue, jsFunc, 0, nullptr, nullptr);
}

AnimatorOptionNapi::~AnimatorOptionNapi()
{
    NapiUtils::DeleteReferences(env_, {jsThis_, onStart_, onFinish_, onPause_, onCancel_, onRepeat_});
}

}
